<?php
/**
 * Logger
 *
 * @author den
 *
 */
class Lemar_Logger
{
	/**
	 * Config file name
	 *
	 * @var string
	 */
	private static $_sConfigFile = '../application/configs/application.ini';

	/**
	 * Log file names
	 *
	 * @var array
	 */
	private static $_aLogFiles = array(
		Zend_Log::EMERG  => 'emerg.log',
		Zend_Log::ALERT  => 'alert.log',
		Zend_Log::CRIT   => 'crit.log',
		Zend_Log::ERR    => 'err.log',
		Zend_Log::WARN   => 'warn.log',
		Zend_Log::NOTICE => 'notice.log',
		Zend_Log::INFO   => 'info.log',
		Zend_Log::DEBUG  => 'debug.log',
	);

	/**
	 * Logs into file
	 *
	 * @param string $sMessage
	 * @param int $iPriority
	 */
	public static function log($sMessage, $iPriority = Zend_Log::ERR)
	{
		$sEnv = APPLICATION_ENV;
		$oConfig = Zend_Registry::getInstance()->get('config')->$sEnv;

		if ($oConfig->firephplogger->enabled)
		{
			$oWriter = new Zend_Log_Writer_Firebug();
		}
		else
		{
			$sLogDir = (null !== $oConfig->logdir) ? $oConfig->logdir : '/log/';
			$sFileName = isset(self::$_aLogFiles[$iPriority]) ? self::$_aLogFiles[$iPriority] : 'err.log';
			$oWriter = new Zend_Log_Writer_Stream($sLogDir.$sFileName);
		}

		$oLogger = new Zend_Log();
		$oLogger->addWriter($oWriter);

		$iPriority = (int) $iPriority;

		$oLogger->log($sMessage, $iPriority);
	}

	/**
	 * Outputs data to browser
	 *
	 * @param mixed $mData
	 * @param bool $stop
	 */
	public static function toBrowser($mData, $stop = true)
	{
		echo '<pre>';
		echo htmlentities(print_r($mData, true), ENT_QUOTES, 'UTF-8');
		echo '</pre>';
		if ($stop) {
			die();
		}
	}

	/**
	 * Saves paypal request to db
	 *
	 * @param string $sUserEmail
	 * @param array $aRequest
	 */
	public static function logPayPalRequest($sUserEmail, $oRequest)
	{
		$sRequestDateTime = Lemar_Date::getCurrentDate(Lemar_Date::MYSQL_DATETIME);

		$sTransactionId = $oRequest->getParam('txn_id');
		$aPost = (array) $oRequest->getPost();

		$oPaypalRequestLog = new models_PaypalRequestLog();
		$oPaypalRequestLog->setUserEmail($sUserEmail)
		                  ->setTransactionId($sTransactionId)
		                  ->setRequestData(serialize($aPost))
		                  ->setRequestDateTime($sRequestDateTime);
		models_PaypalRequestLogMapper::save($oPaypalRequestLog);
	}
}
